home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr07 / oleo130s.zip / OLEO130S.TAR / oleo-1.3 / args.c < prev    next >
C/C++ Source or Header  |  1993-03-30  |  10KB  |  496 lines

  1. /*    Copyright (C) 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. #include <ctype.h>
  18. #include "global.h"
  19. #define DEFINE_STYLES    1
  20. #include "args.h"
  21. #include "cmd.h"
  22. #include "io-abstract.h"
  23. #include "io-generic.h"
  24. #include "io-edit.h"
  25. #include "io-utils.h"
  26. #include "format.h"
  27.  
  28.  
  29.  
  30. /* These commands define the syntax and editting modes of command arguments.
  31.  * Each _verify function parses some kind of argument and stores its value
  32.  * in a command_arg structure.  An error message or NULL is returned.
  33.  * If the match succeeds, a string pointer is advanced to the end of what was
  34.  * parsed.
  35.  *
  36.  * Several arguments may be listed on one line separated only by
  37.  * whitespace.  A _verify function should stop after its argument and ignore
  38.  * everything after that. 
  39.  */
  40.  
  41. #ifdef __STDC__
  42. char *
  43. char_verify (char ** end, struct command_arg * arg)
  44. #else
  45. char *
  46. char_verify (end, arg)
  47.      char ** end;
  48.      struct command_arg * arg;
  49. #endif
  50. {
  51.   if (!**end)
  52.     return "No character specified";
  53.   else
  54.     {
  55.       int ch = string_to_char (end);
  56.       if (ch < 0)
  57.     {
  58.       setn_edit_line ("", 0);
  59.       return "Illegal character constant.";
  60.     }
  61.       else
  62.     {
  63.       arg->val.integer = ch;
  64.       return 0;
  65.     }
  66.     }
  67. }
  68.  
  69. #ifdef __STDC__
  70. char *
  71. symbol_verify (char ** end, struct command_arg * arg)
  72. #else
  73. char *
  74. symbol_verify (end, arg)
  75.      char ** end;
  76.      struct command_arg * arg;
  77. #endif
  78. {
  79.   char * e = *end;
  80.   char * start = *end;
  81.   if (isalpha (*e) || (*e == '-') || (*e == '_') || (*e == (a0 ? '$' : ':')))
  82.     {
  83.       while (isalpha(*e) || isdigit(*e) || (*e == '-') || (*e == '_')
  84.          || (*e == (a0 ? '$' : ':')))
  85.     ++e;
  86.       if (!isspace(*e) && *e)
  87.     goto bad_symbol;
  88.       *end = e;
  89.       arg->val.string = (char *)ck_malloc (e - start + 1);
  90.       bcopy (start, arg->val.string, e - start);
  91.       arg->val.string[e - start] = '\0';
  92.       return 0;
  93.     }
  94.  bad_symbol:
  95.   if (arg->arg_desc[1] == '\'')
  96.     {
  97.       arg->val.string = 0;
  98.       return 0;
  99.     }
  100.   else
  101.     return "Invalid symbol name.";
  102. }
  103.  
  104. char *
  105. word_verify (end, arg)
  106.      char ** end;
  107.      struct command_arg * arg;
  108. {
  109.   char * e = *end;
  110.   char * start = *end;
  111.   if (!isspace (*e))
  112.     {
  113.       while (!isspace(*e))
  114.     ++e;
  115.       *end = e;
  116.       arg->val.string = (char *)ck_malloc (e - start + 1);
  117.       bcopy (start, arg->val.string, e - start);
  118.       arg->val.string[e - start] = '\0';
  119.       return 0;
  120.     }
  121.   else if (arg->arg_desc[1] == '\'')
  122.     {
  123.       arg->val.string = 0;
  124.       return 0;
  125.     }
  126.   else
  127.     return "Invalid symbol name.";
  128. }
  129.  
  130. #ifdef __STDC__
  131. void
  132. symbol_destroy (struct command_arg * arg)
  133. #else
  134. void
  135. symbol_destroy (arg)
  136.      struct command_arg * arg;
  137. #endif
  138. {
  139.   if (arg->val.string)
  140.     ck_free (arg->val.string);
  141. }
  142.  
  143. #ifdef __STDC__
  144. char *
  145. command_verify (char ** end, struct command_arg * arg)
  146. #else
  147. char *
  148. command_verify (end, arg)
  149.      char ** end;
  150.      struct command_arg * arg;
  151. #endif
  152. {
  153.   char * error = symbol_verify (end, arg);
  154.   char * str;
  155.   if (error)
  156.     return error;
  157.   str = arg->val.string;
  158.   if (!(find_function (0, 0, arg->val.string, strlen(arg->val.string))
  159.         && get_abs_rng (&str, 0)))
  160.     return 0;
  161.   else
  162.     return "Not a command or macro address.";
  163. }
  164.  
  165. #ifdef __STDC__
  166. char * 
  167. read_file_verify (char ** end, struct command_arg * arg)
  168. #else
  169. char * 
  170. read_file_verify (end, arg)
  171.      char ** end;
  172.      struct command_arg * arg;
  173. #endif
  174. {
  175.   FILE * fp = xopen_with_backup (arg->text.buf, "r");
  176.   *end = 0;
  177.   if (!fp)
  178.     {
  179.       io_error_msg ("Can't open file '%s':%s", arg->text.buf, err_msg ());
  180.       return "";
  181.     }
  182.   else
  183.     {
  184.       arg->val.fp = fp;
  185.       return 0;
  186.     }
  187. }
  188.  
  189. #ifdef __STDC__
  190. void
  191. read_file_destroy (struct command_arg * arg)
  192. #else
  193. void
  194. read_file_destroy (arg)
  195.      struct command_arg * arg;
  196. #endif
  197. {
  198.   int num;
  199.   num = xclose (arg->val.fp);
  200.   if (num)
  201.     io_error_msg ("Can't close '%s': Error code %d: %s",
  202.           arg->text.buf, num, err_msg ());
  203. }
  204.  
  205.  
  206. #ifdef __STDC__
  207. char * 
  208. write_file_verify (char ** end, struct command_arg * arg)
  209. #else
  210. char * 
  211. write_file_verify (end, arg)
  212.      char ** end;
  213.      struct command_arg * arg;
  214. #endif
  215. {
  216.   FILE * fp = xopen_with_backup (arg->text.buf, "w");
  217.   *end = 0;
  218.   if (!fp)
  219.     {
  220.       io_error_msg ("Can't open file '%s':%s", arg->text.buf, err_msg ());
  221.       return "";
  222.     }
  223.   else
  224.     {
  225.       arg->val.fp = fp;
  226.       return 0;
  227.     }
  228. }
  229.  
  230. #ifdef __STDC__
  231. void
  232. write_file_destroy (struct command_arg * arg)
  233. #else
  234. void
  235. write_file_destroy (arg)
  236.      struct command_arg * arg;
  237. #endif
  238. {
  239.   int num;
  240.  
  241.   num = xclose (arg->val.fp);
  242.   if (num)
  243.     io_error_msg ("Can't close '%s': Error code %d: %s",
  244.           arg->text.buf, num, err_msg ());
  245. }
  246.  
  247. /* As a special case, cmd_loop makes sure that keyseq arguments are only read
  248.  * interactively.
  249.  */
  250.  
  251. #ifdef __STDC__
  252. char *
  253. keyseq_verify (char ** end, struct command_arg * arg)
  254. #else
  255. char *
  256. keyseq_verify (end, arg)
  257.      char ** end;
  258.      struct command_arg * arg;
  259. #endif
  260. {
  261.   *end = 0;
  262.   return 0;
  263. }
  264.  
  265.  
  266. #ifdef __STDC__
  267. char *
  268. keymap_verify (char ** end, struct command_arg * arg)
  269. #else
  270. char *
  271. keymap_verify (end, arg)
  272.      char ** end;
  273.      struct command_arg * arg;
  274. #endif
  275. {
  276.   char * start = *end;
  277.   char * error = symbol_verify (end, arg);
  278.   int id;
  279.   if (error)
  280.     return error;
  281.   id = map_idn (start, *end - start);
  282.   return (id >= 0
  283.       ? (char *) 0
  284.       : "No such keymap.");
  285. }
  286.  
  287.  
  288. #ifdef __STDC__
  289. char *
  290. number_verify (char ** end, struct command_arg * arg)
  291. #else
  292. char *
  293. number_verify (end, arg)
  294.      char ** end;
  295.      struct command_arg * arg;
  296. #endif
  297. {
  298.   char * e = *end;
  299.  
  300.   while (isspace (*e))
  301.     ++e;
  302.   if (isdigit(*e) || (*e == '-'))
  303.     {
  304.       arg->val.integer = astol (end);
  305.       if (arg->arg_desc[1] == '[')
  306.     {
  307.       char * prompt = arg->arg_desc + 2;
  308.       {
  309.         long low = 0;
  310.         long high = -1;
  311.         low = astol (&prompt);
  312.         while (isspace (*prompt))  ++prompt;
  313.         if (*prompt == ',') ++prompt;
  314.         high = astol (&prompt);
  315.         while (isspace (*prompt))  ++prompt;
  316.         if (*prompt == ']') ++prompt;
  317.         if (   (low > arg->val.integer)
  318.         || (high < arg->val.integer))
  319.           io_error_msg ("Out of range %d (should be in [%d - %d]).",
  320.                 arg->val.integer, low, high); /* no return */
  321.       }
  322.     }
  323.       return 0;
  324.     }
  325.   else
  326.     return "Not a number.";
  327. }
  328.  
  329.  
  330. #ifdef __STDC__
  331. char *
  332. double_verify (char ** end, struct command_arg * arg)
  333. #else
  334. char *
  335. double_verify (end, arg)
  336.      char ** end;
  337.      struct command_arg * arg;
  338. #endif
  339. {
  340.   char * e = *end;
  341.  
  342.   while (isspace (*e))
  343.     ++e;
  344.   if (isdigit(*e) || ((*e == '-') && isdigit (*(e + 1))))
  345.     {
  346.       arg->val.floating = astof (end);
  347.       return 0;
  348.     }
  349.   else
  350.     return "Not a number.";
  351. }
  352.  
  353.  
  354. #ifdef __STDC__
  355. char * 
  356. range_verify (char ** end, struct command_arg * arg)
  357. #else
  358. char * 
  359. range_verify (end, arg)
  360.      char ** end;
  361.      struct command_arg * arg;
  362. #endif
  363. {
  364.   union command_arg_val * val = &arg->val;
  365.   *end = arg->text.buf;
  366.   if (get_abs_rng (end, &val->range))
  367.     return "Not a range.";
  368.   else
  369.     return 0;
  370. }
  371.  
  372. #ifdef __STDC__
  373. char * 
  374. string_verify (char ** end, struct command_arg * arg)
  375. #else
  376. char * 
  377. string_verify (end, arg)
  378.      char ** end;
  379.      struct command_arg * arg;
  380. #endif
  381. {
  382.   arg->val.string = arg->text.buf;
  383.   *end = 0;
  384.   return 0;
  385. }
  386.  
  387. /* Unlike most verify functions, this
  388.  * one may destroy the command frame that it is 
  389.  * operating on.  It's purpose is to allow user's
  390.  * to abort commands. 
  391.  */
  392. #ifdef __STDC__
  393. char * 
  394. yes_verify (char ** end, struct command_arg * arg)
  395. #else
  396. char * 
  397. yes_verify (end, arg)
  398.      char ** end;
  399.      struct command_arg * arg;
  400. #endif
  401. {
  402.   if (words_imatch (end, "no"))
  403.     {
  404.       pop_unfinished_command ();
  405.       return "Aborted.";
  406.     }
  407.   else if (words_imatch (end, "yes"))
  408.     return 0;
  409.   else
  410.     {
  411.       setn_edit_line ("", 0);
  412.       return "Please answer yes or no.";
  413.     }
  414. }
  415.  
  416. #ifdef __STDC__
  417. char *
  418. incremental_cmd_verify (char ** end, struct command_arg * arg)
  419. #else
  420. char *
  421. incremental_cmd_verify (end, arg)
  422.      char ** end;
  423.      struct command_arg * arg;
  424. #endif
  425. {
  426.   return 0;
  427. }
  428.  
  429.  
  430. #ifdef __STDC__
  431. char *
  432. menu_verify (char ** end, struct command_arg * arg)
  433. #else
  434. char *
  435. menu_verify (end, arg)
  436.      char ** end;
  437.      struct command_arg * arg;
  438. #endif
  439. {
  440.   char * error = char_verify (end, arg);
  441.   if (error)
  442.     return error;
  443.  
  444.   {
  445.     int pick = arg->val.integer;
  446.     char * key = arg->arg_desc + 1;
  447.     while (*key && (*key != ']'))
  448.       {
  449.     if (*key == '\\')
  450.       {
  451.         ++key;
  452.         if (!*key)
  453.           break;
  454.       }
  455.     if (pick == *key)
  456.       return 0;
  457.     else
  458.       ++key;
  459.       }
  460.     setn_edit_line ("", 0);
  461.     return "No such menu option.";
  462.   }
  463. }
  464.  
  465.  
  466. #ifdef __STDC__
  467. char *
  468. format_verify (char ** end, struct command_arg * arg)
  469. #else
  470. char *
  471. format_verify (end, arg)
  472.      char ** end;
  473.      struct command_arg * arg;
  474. #endif
  475. {
  476.   arg->val.integer = str_to_fmt (*end);
  477.   if (arg->val.integer < 0)
  478.     return "Unknown format.";
  479.   *end = 0;
  480.   return 0;
  481. }
  482.  
  483.  
  484. #ifdef __STDC__
  485. char *
  486. noop_verify (char ** end, struct command_arg * arg)
  487. #else
  488. char *
  489. noop_verify (end, arg)
  490.      char ** end;
  491.      struct command_arg * arg;
  492. #endif
  493. {
  494.   return 0;
  495. }
  496.